﻿using SpeechLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

using System.Threading;


DSpeech dSpeech = new DSpeech();
var pi = Math.PI;
dSpeech.tttt(pi.ToString());

public class DSpeech
{
    private static readonly DSpeech _instance = new DSpeech();

    private static object objlock = new object();
    public static DSpeech Instance
    {
        get
        {
            return _instance;
        }
    }
    public const int SND_FILENAME = 0x00020000;
    public const int SND_ASYNC = 0x0001;

    [DllImport("winmm.dll")]
    public static extern bool PlaySound(string pszSound, int hmod, int fdwSound);

    public void AsyncSpeech(string text)
    {
        Task.Factory.StartNew(() => TextToVoice(text));
    }

    public void tttt(string text)
    {
        TextToVoice(text);
    }
    private void TextToVoice(string text)
    {
        lock (objlock)
        {
            try
            {
                string soundPath =  "\\嘀嘀嘀.wav";
                Sound(soundPath);
                SpVoice spVoice = new SpVoice();
                spVoice.Rate = 1;
                spVoice.Volume = 100;
                spVoice.Speak(text);
            }
            catch (Exception)
            {
            }
            Thread.Sleep(2000);
        }
    }

    private void Sound(string soundPath)
    {
        try
        {
            PlaySound(soundPath, 0, SND_FILENAME);
        }
        catch
        {
        }
    }
}
